<?php
/**
 * Created by PhpStorm.
 * User: lijie
 * Date: 2018/10/7
 * Time: 9:02
 */
namespace app\reserve\model;

use think\Model;
use think\Db;

class Orderwash extends Model{

    protected $table = "orange_orderwash";
    protected $autoWriteTimestamp = true;
    protected $createTime = "ctime";
    protected $updateTime = "utime";
    protected $dateFormat;
    protected $insert = ['status'=>1];

    /**
     * Notes:  添加
     * Create Date: 2018/10/5
     * Create by :lijie
     */
    public function addOrder($param)
    {
        return $this->isUpdate(false)->allowField(true)->save($param);
    }

    /**
     * Notes:  删除
     * Create Date: 2018/10/5
     * Create by :lijie
     */
    public function delOrder($param)
    {
        return $this->isUpdate(true)->save(['status'=>9],['id'=>$param]);
    }

    /**
     * Notes:  编辑
     * Create Date: 2018/10/5
     * Create by :lijie
     */
    public function editOrder($param,$where)
    {
        unset($param['id']);
        return $this->isUpdate(true)->allowField(true)->save($param,$where);
    }

    /**
     * Notes:  列表
     * Create Date: 2018/10/5
     * Create by :lijie
     */
    public function selectOrder($where,$pagenum,$listNum)
    {
        return $this
            ->alias("o")
            ->where($where)
            ->field("o.*,a.name cash_name,u.nickName user_name,w.name wash_name,w.address,w.contactnum tel,w.pic")
            ->join("__CASH__ a"         ,"o.a_id = a.a_id")
            ->join("__USER__ u"         ,"o.u_id = u.u_id")
            ->join("__WASHCASH__ w"     ,"o.wash_id = w.washid")
//            ->join("__WASHPRODUCT__ p"  ,"o.washpro_id = p.id")
            ->order("o.ctime desc")->page($pagenum,$listNum)->select();
    }

    /**
     * Notes:  详情
     * Create Date: 2018/10/5
     * Create by :lijie
     */
    public function findOrder($where)
    {
//        return $this->where($where)->find();
        $res = $this
            ->alias("o")
            ->where($where)
            ->field("o.*,a.name cash_name,u.nickName user_name,w.name wash_name")
            ->join("__CASH__ a"         ,"o.a_id = a.a_id")
            ->join("__USER__ u"         ,"o.u_id = u.u_id")
            ->join("__WASHCASH__ w"     ,"o.wash_id = w.washid")
            ->select();
        return $res;
    }
    /*
    * Notes:
    * Create Date: 2018/10/7
    * Create Time: 下午3:26
    * Create by :张福来
    * param :
    */
    public function makesureOrder($ordernumber,$postData){
        Db::startTrans();
        $result = $this
                ->save(["status"=>2],["ordernumber"=>$ordernumber]);
        $order = $this
                ->where(["ordernumber"=>$ordernumber])
                ->find();
        $cardwallet = Db::name("onecard")
                    ->where(["cardnumber"=>$order["onecard"]])
                    ->find();
        if(($cardwallet["wallet"] - $order["pay_price"])<0){
            Db::rollback();
            return false;
        }
        $result2 = Db::name("onecard")
                ->where(["u_id"=>$order["u_id"]])
                ->setDec("wallet",$order["pay_price"]);
        $result3 = Db::name("washcash")
                ->where(["washid"=>$order["wash_id"]])
                ->setInc("wallet",$order["realget"]);
        $washrecord["u_id"] = $order["u_id"];
        $washrecord["washid"] = $order["wash_id"];
        $washrecord["content"] = "洗车消费".$order["pay_price"]."元";
        $washrecord["price"] = $order["pay_price"];
        $washrecord["ctime"] = time();
        $washrecord["utime"] = time();
        $washrecord["status"] = 0;
        $washbill["washid"] =  $order["wash_id"];
        $washbill["u_id"] =  $order["u_id"];
        $washbill["price"] =  $order["realget"];
        $washbill["lessorad"] =  0;
        $washbill["content"] =  "订单".$order["ordernumber"].",已完成";
        $washbill["ctime"] = time();
        $washbill["utime"] = time();
        $washbill["status"] = 0;
        $result4 = Db::name("washrecord")
                ->insert($washrecord);
        $result5 = Db::name("washcashbill")
            ->insert($washbill);
        if($result && $result2 && $result3 && $result4 && $result5){
            Db::commit();
            return true;
        }else{
            Db::rollback();
            return false;
        }

    }

    public function dataScan($w,$wh){
        $totalprice =  $this
                    ->field("SUM(realget) totalprice")
                    ->where($w)
                    ->find();
        $result["totalcount"] =  $this
            ->where($w)
            ->count();
        $yesterdayprice = $this
            ->field("SUM(realget) yesterdayprice")
            ->where($w)
            ->where($wh)
            ->find();
        $result["totalprice"] = $totalprice["totalprice"];
        $result["yesterdayprice"] = $yesterdayprice["yesterdayprice"];
        return $result;

    }
}